package cn.stylefeng.guns.modular.api;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.core.common.annotion.Permission;
import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import cn.stylefeng.guns.modular.system.entity.Coupon;
import cn.stylefeng.guns.modular.system.model.CouponDto;
import cn.stylefeng.guns.modular.system.service.CouponService;
import cn.stylefeng.guns.modular.system.warpper.CouponWarpper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * 优惠卷控制器
 */
@Controller
@RequestMapping("/api/coupon")
public class CouponApiController extends BaseController {
    private static String PREFIX = "/modular/system/coupon/";

    @Autowired
    private CouponService couponService;

    @RequestMapping(value = "/coupon_fill_out")
    public String couponFillOut() {
        return PREFIX + "coupon_fill_out.html";
    }


    /**
     * 填写兑换卷(api)
     */
    @RequestMapping(value = "/fillOut")
    @ResponseBody
    public ResponseData couponFillOut(@Valid CouponDto couponDto, BindingResult result) {
        if (result.hasErrors()) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        this.couponService.couponFillOut(couponDto);
        return SUCCESS_TIP;
    }

    /**
     * 查看兑换卷(api)
     */
    @RequestMapping(value = "/confirmation")
    @ResponseBody
    public ResponseData couponFillOut(@RequestParam String couponSN,@RequestParam String phone) {

        Map<String, Object> couponMap = BeanUtil.beanToMap(couponService.selectBySnAndphone(couponSN,phone));
        return ResponseData.success(couponMap);
    }
}
